package com.elock.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.JsonUtils;
import com.elock.entity.Car;
import com.elock.entity.CarSaleOrg;
import com.elock.service.CarSaleOrgService;
import com.elock.service.CarService;
import com.elock.service.ElockCarService;
import com.elock.service.SaleOrgBaseService;

/**
 * Controller - 车辆
 * @author 仁德
 */
@Controller("carController")
@RequestMapping("/car")
public class CarController extends BaseController {

	@Resource(name = "carServiceImpl")
	private CarService carService;
	@Resource(name = "elockCarServiceImpl")
	private ElockCarService elockCarService;
	@Resource(name = "carSaleOrgServiceImpl")
	private CarSaleOrgService carSaleOrgService;
	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list_tb", method = RequestMethod.GET)
	public String list_tb(ModelMap model) {

		return "/car/list_tb";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Pageable pageable, ModelMap model) {

		return "/car/list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list_data(String carNumber, String carType,
			Long saleOrgId, Pageable pageable, ModelMap model) {

//		List<Filter> filters = new ArrayList<Filter>();
//		if (!ConvertUtil.isEmpty(carNumber)) {
//			filters.add(Filter.like("carNumber", "%" + carNumber + "%"));
//		}
//		if (!ConvertUtil.isEmpty(carType)) {
//			filters.add(Filter.like("carType", "%" + carType + "%"));
//		}
//		Page<Car> page = carService.findPage(filters, null, pageable);

		Page<Map<String, Object>> page = carService.findPage(carNumber,
				carType,
				saleOrgId,
				pageable);
		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {

		model.addAttribute("saleOrg", saleOrgBaseService.getCurrentSaleOrg());
		return "/car/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg save(Car car, Long saleOrgId, Long[] elockId,
			Integer[] berth, String[] position) {

		if (ConvertUtil.isEmpty(car.getCarNumber())) {
			return error("请填写车牌号");
		}
		Car c = carService.findByCarNumber(car.getCarNumber());
		if (c != null) {
			return error("该车牌号在系统中已存在");
		}
		carService.saveUpdate(car, saleOrgId, elockId, berth, position);

		return success().addObjX(car.getId());
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {

		Car car = carService.find(id);
		model.addAttribute("car", car);

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("carId", id));
//		List<ElockCar> elockCars = elockCarService.findList(null, filters, null);
//		model.addAttribute("elockCars", elockCars);

		List<Map<String, Object>> elockCars = carService.findElockCarByCarId(id);
		model.addAttribute("elockCars", JsonUtils.toJson(elockCars));

		filters.clear();
		filters.add(Filter.eq("car", car));
		CarSaleOrg carSaleOrg = carSaleOrgService.find(filters);
		if (carSaleOrg != null) {
			model.addAttribute("saleOrg", carSaleOrg.getSaleOrg());
		}
		else {
			model.addAttribute("saleOrg", saleOrgBaseService.getCurrentSaleOrg());
		}

		return "/car/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg update(Car car, Long saleOrgId, Long[] elockId,
			Integer[] berth, String[] position) {

		if (ConvertUtil.isEmpty(car.getCarNumber())) {
			return error("请填写车牌号");
		}
		Car c = carService.findByCarNumber(car.getCarNumber());
		if (c != null && c.getId() != car.getId()) {
			return error("该车牌号在系统中已存在");
		}
		carService.saveUpdate(car, saleOrgId, elockId, berth, position);

		return success();
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody ResultMsg delete(Long[] ids) {

		carService.delete(ids);
		return success();

	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/select_list", method = RequestMethod.GET)
	public String select_list(Pageable pageable, Integer multi, ModelMap model) {

		model.addAttribute("multi", multi);
		return "/car/select_list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/select_list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg select_list_data(String carNumber, String carType,
			Long saleOrgId, Pageable pageable, ModelMap model) {

		Page<Map<String, Object>> page = carService.findPage(carNumber,
				carType,
				saleOrgId,
				pageable);
		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}

	@RequestMapping(value = "/select_elock_car_list", method = RequestMethod.GET)
	public String select_elock_car_list(Pageable pageable, Integer multi,
			ModelMap model) {

		model.addAttribute("multi", multi);
		return "/car/select_elock_car_list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/select_elock_car_list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg select_elock_car_list_data(String carNumber,
			String carType, String elockNum, String elockName, Pageable pageable,
			ModelMap model) {

		Page<Map<String, Object>> page = carService.findElockCar(carNumber,
				carType,
				elockNum,
				elockName,
				pageable);
		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}

	@RequestMapping(value = "/getElockCar", method = RequestMethod.POST)
	public @ResponseBody ResultMsg getElockCar(Long carId) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("carId", carId));
		List<Map<String, Object>> list = carService.findElockCarByCarId(carId);

		return success().addObjX(list);
	}
}
